define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'customer/deal/index' + location.search,
                    add_url: 'customer/deal/add',
                    edit_url: 'customer/intention/edit',
                    del_url: 'customer/deal/del',
                    multi_url: 'customer/deal/multi',
                    import_url: 'customer/deal/import',
                    table: 'customer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: (params) => {
                    var filter = JSON.parse(params.filter)
                    var op = JSON.parse(params.op)

                    filter.customer_type = 1
                    op.customer_type = '='

                    params.filter = JSON.stringify(filter)
                    params.op = JSON.stringify(op)

                    return params
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'customer_identity',
                            title: '业务类型',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.status,
                            custom: {1: "green"},
                            searchList: {0: '个人', 1: '公司'}
                        },
                        {
                            field: 'full_name',
                            title: '客户姓名',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'idnumber',
                            title: '身份证号',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'corporate_name',
                            title: '公司名称',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'signatory',
                            title: '授权签约人',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        // {
                        //     field: 'status',
                        //     title: '审核状态',
                        //     formatter: Table.api.formatter.status,
                        //     custom: {1: "orange", 2: "aqua", 3: 'red'},
                        //     searchList: {0: '待提交', 1: '审核中', 2: '审核通过', 3: '审核不通过'}
                        // },
                        // {
                        //     field: 'ht_status',
                        //     title: '合同状态',
                        //     custom: {0: "red", 1: "green",},
                        //     searchList: {0: '未签名', 1: '客户已签名'},
                        //     formatter: Table.api.formatter.status,
                        // },
                        // {field: 'customer_type', title: __('Customer_type')},
                        {
                            field: 'corporate_type',
                            title: __('Corporate_type'),
                            formatter: Table.api.formatter.status,
                            custom: {1: "green", 2: "red"},
                            searchList: {0: '个体户', 1: '小规模纳税人', 2: '一般纳税人'}
                        },
                        {field: 'region', title: __('Region'), operate: 'LIKE'},
                        {
                            field: 'total_amount',
                            title: '服务总金额',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'admin_info.nickname',
                            title: '销售名称',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,

                            events: Table.api.events.operate,
                            buttons: [
                                // {
                                //     name: 'deal',
                                //     text: __('标记为成交客户'),
                                //     title: __('标记为成交客户'),
                                //     url: 'customer/intention/deal',
                                //     classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                //     success: function (data, ret) {
                                //         table.bootstrapTable('refresh');
                                //     },
                                //     error: function (data, ret) {
                                //         Layer.alert(ret.msg);
                                //         return false;
                                //     }
                                // },
                                {
                                    name: 'addtabs',
                                    text: __('服务列表'),
                                    title: __('服务列表'),
                                    extend: 'data-area=\'["80%","80%"]\'',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'customer/package/index',
                                },
                                // {
                                //     name: 'addtabs',
                                //     text: __('查看合同'),
                                //     title: __('查看合同'),
                                //     classname: 'btn btn-xs btn-warning btn-addtabs',
                                //     // classname: 'btn btn-xs btn-warning btn-dialog',
                                //     url: 'customer/intention/previewContract',
                                //     // visible: function (row) {
                                //     //     if (Config['ckht']) {
                                //     //         return true
                                //     //     } else {
                                //     //         return false
                                //     //     }
                                //     // },
                                // },
                                // {
                                //     name: 'addtabs',
                                //     text: __('签订合同'),
                                //     title: __('签订合同'),
                                //     classname: 'btn btn-xs btn-warning btn-dialog',
                                //     url: 'customer/intention/signatureQrcode',
                                //     // visible: function (row) {
                                //     // if (Config['qdht'] && !row.signature_url) {
                                //     //     return true
                                //     // } else {
                                //     //     return false
                                //     // }
                                //     // },
                                // },
                                // {
                                //     visible: function (row) {
                                //         if (Config['cjxx'] && row.status == 0) {
                                //             return true
                                //         } else {
                                //             return false
                                //         }
                                //     },
                                //     name: 'addtabs',
                                //     text: __('成交客户'),
                                //     title: __('成交客户'),
                                //     classname: 'btn btn-xs btn-success btn-dialog',
                                //     url: 'customer/intention/deal'
                                // },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                onLoadSuccess: function () {
                    var cookiefilenames = 'customer_index_controller';  //对应控制器列表页
                    table.on('column-switch.bs.table', function (e, json) {
                        var hideColumns = [];
                        $.each(table.bootstrapTable('getHiddenColumns'), function (i, item) {
                            hideColumns.push(item.field);
                        });
                        $.cookie('texie_' + cookiefilenames, JSON.stringify(hideColumns), {expires: 365, path: '/'});
                    });
                    if ($.cookie('texie_' + cookiefilenames)) {
                        $.each(JSON.parse($.cookie('texie_' + cookiefilenames)), function (i, item) {
                            table.bootstrapTable('hideColumn', item);
                        });
                    }
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        fpkj: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
